package com.kinghood.authoritycenter.controller.authority;


import com.kinghood.authoritycenter.common.model.dao.authority.AuthorityRoleDO;
import com.kinghood.authoritycenter.common.model.dto.authority.*;
import com.kinghood.authoritycenter.common.model.dto.common.IdListReqDTO;
import com.kinghood.authoritycenter.common.model.vo.authority.GetRoleVO;
import com.kinghood.authoritycenter.service.IAuthorityRoleService;
import com.kinghood.infrastructure.api.authority.SysLogModuleEnum;
import com.kinghood.infrastructure.aspect.CustomCacheEvict;
import com.kinghood.infrastructure.aspect.OperatorLog;
import com.kinghood.infrastructure.model.KingHoodResponseVO;
import com.kinghood.infrastructure.model.constant.redis.RedisConstant;
import com.kinghood.sharding.model.PageResponseVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * 组织
 *
 * @author zhangFanJun
 * @date 2023-12-06 10:42
 **/
@RequestMapping("/role")
@Slf4j
@RequiredArgsConstructor
@RestController
public class RoleController {

    private final IAuthorityRoleService roleService;

    /**
     * 角色分页数据
     *
     * @param in 筛选调价
     * @return 角色分页数据
     * @author zfj
     * @date 2023/12/18
     */
    @PostMapping("/queryPage")
    public KingHoodResponseVO<PageResponseVO<AuthorityRoleDO>> queryPage(@RequestBody RoleQueryPageReqDTO in) {
        return KingHoodResponseVO.success(roleService.queryPage(in));
    }

    /**
     * 角色信息修改
     *
     * @param in 角色信息
     * @return 角色信息
     * @author zfj
     * @date 2023/12/18
     */
    @CustomCacheEvict(RedisConstant.AUTH)
    @OperatorLog(module = SysLogModuleEnum.Type.AUTHORITY_ROLE,content = "角色管理-保存角色信息")
    @PostMapping("/addOrUpdateRole")
    public KingHoodResponseVO<AuthorityRoleDO> addOrUpdateRole(@RequestBody UpdateRoleReqDTO in) {
        if(Objects.isNull(in.getId())){
            AddRoleReqDTO reqDTO = new AddRoleReqDTO();
            BeanUtils.copyProperties(in,reqDTO);
            return KingHoodResponseVO.success(roleService.addRole(reqDTO));
        }
        return KingHoodResponseVO.success(roleService.updateRole(in));
    }

    /**
     * 新建角色
     *
     * @param in 角色信息
     * @return 角色信息
     * @author zfj
     * @date 2023/12/18
     */
    @CustomCacheEvict(RedisConstant.AUTH)
    @OperatorLog(module = SysLogModuleEnum.Type.AUTHORITY_ROLE,content = "角色管理-新建角色")
    @PostMapping("/addRole")
    public KingHoodResponseVO<AuthorityRoleDO> addRole(@Valid @RequestBody AddRoleReqDTO in) {
        return KingHoodResponseVO.success(roleService.addRole(in));
    }

    /**
     * 获取角色信息
     *
     * @param id 角色信息id
     * @return 角色信息
     * @author zfj
     * @date 2023/12/18
     */
    @GetMapping("/getRole/{id}")
    public KingHoodResponseVO<GetRoleVO> getRole(@PathVariable("id") Long id) {
        return KingHoodResponseVO.success(roleService.getRole(id));
    }


    /**
     * 角色信息修改
     *
     * @param in 角色信息
     * @return 角色信息
     * @author zfj
     * @date 2023/12/18
     */
    @CustomCacheEvict(RedisConstant.AUTH)
    @OperatorLog(module = SysLogModuleEnum.Type.AUTHORITY_ROLE,content = "角色管理-角色信息修改")
    @PostMapping("/updateRole")
    public KingHoodResponseVO<AuthorityRoleDO> updateRole(@Valid @RequestBody UpdateRoleReqDTO in) {
        return KingHoodResponseVO.success(roleService.updateRole(in));
    }

    /**
     * 批量删除角色信息
     *
     * @param in 角色id
     * @return 是否删除成功
     * @author zfj
     * @date 2023/12/18
     */
    @CustomCacheEvict(RedisConstant.AUTH)
    @OperatorLog(module = SysLogModuleEnum.Type.AUTHORITY_ROLE,content = "角色管理-批量删除角色信息")
    @PostMapping("/deleteRoleBatch")
    public KingHoodResponseVO<Boolean> deleteRoleBatch(@Valid @RequestBody IdListReqDTO in) {
        return KingHoodResponseVO.success(roleService.deleteRoleBatch(in.getIdList()));
    }

    /**
     * 修改角色状态
     *
     * @param in 入参
     * @return 修改结果
     * @author zfj
     * @date 2023/12/19
     */
    @CustomCacheEvict(RedisConstant.AUTH)
    @OperatorLog(module = SysLogModuleEnum.Type.AUTHORITY_ROLE,content = "角色管理-修改角色状态")
    @PostMapping("/updateStatus")
    public KingHoodResponseVO<Boolean> updateStatus(@RequestBody UpdateRoleStatusReqDTO in) {
        return KingHoodResponseVO.success(roleService.updateStatus(in.getId(), in.getStatus()));
    }

    /**
     * <p>下拉列</p>
     * 查找所有角色
     *
     * @return 查找结果
     * @author zfj
     * @date 2023/12/19
     */
    @PostMapping("/getRoleList")
    public KingHoodResponseVO<List<AuthorityRoleDO>> getRoleList() {
        return KingHoodResponseVO.success(roleService.getRoleList());
    }
}
